package com.infinitus.activity.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.infinitus.activity.actenum.QualiStateEnum;
import com.infinitus.activity.common.ResponseCodeConstant;
import com.infinitus.activity.pojo.DO.AuldrawVo;
import com.infinitus.activity.pojo.VO.*;
import com.infinitus.activity.service.AuldrawWinnerService;
import com.infinitus.activity.service.ConfigService;
import com.infinitus.activity.service.DistributionLockService;
import com.infinitus.activity.service.impl.LotteryService;
import com.infinitus.activity.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @create: 2019/1/9
 * @desc:
 */
@RestController
@RequestMapping("/lottery")
public class LotteryController {

    private Logger logger = LoggerFactory.getLogger(LotteryController.class);

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisTemplate<String, String> stringRedisTemplate;

    @Resource
    DistributionLockService distributionLockService;

    @Autowired
    AuldrawWinnerService auldrawWinnerService;

    @Autowired
    ConfigService configService;

    @Autowired
    private ActivityConfigVO activityConfig;

    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Value("${qualiDefNo}")
    private String qualiDefNo;    //资格自定义编码,唯一

    @Value("${deploycolId}")
    private String deploycolId;     //活动部署FkId

    @Autowired
    private LotteryService lotteryService;

    /**
     * 对外抽奖接口
     *
     * @param dealerNo
     * @return
     */
    @RequestMapping(path = "/act", method = RequestMethod.GET)
    public ResponseEntity<?> lottery( @RequestParam String dealerNo) throws Exception {
        //是否登录检查
        Integer responseCode = lotteryService.lotteryCheck(dealerNo, activityConfig);
        if (null != responseCode) {
            return ResponseEntity.ok(new ResponseVO<>().setCode(responseCode));
        }
        ResponseVO responseVO= lotteryService.lottery(dealerNo);
        if(null!=responseVO){
            return ResponseEntity.ok(responseVO);
        }
        return ResponseEntity.ok(new ResponseVO<>().setCode(ResponseCodeConstant.ERROR_CODE).setContent("抽奖出现错误！请联系管理员"));
    }







}